//
// Created by Administrator on 2022/11/8.
//
#ifndef HTTPSERVER_HTTP_MULTI_IO_H
#define HTTPSERVER_HTTP_MULTI_IO_H
#ifndef SOCKET_SERVER_SELECT
#define SELECTRDNEW	0x2000
#define SELECTWTNEW	0x1000
//#define SOCKET_SERVER_SELECT
#endif
#if !defined(SOCKET_SERVER_SELECT) && !defined(SOCKET_SERVER_POLL)
#define POLLRDNEW	0x2000
#define POLLWTNEW	0x1000
//#define SOCKET_SERVER_POLL
#endif
#if !defined(SOCKET_SERVER_SELECT) && !defined(SOCKET_SERVER_POLL) && !defined(SOCKET_SERVER_EPOLL)
#define SOCKET_SERVER_EPOLL 1
#endif
#include "http_socket.h"

typedef struct event_params_s {
    int fd;
    void* args;
}event_p_t;

typedef void (*fd_callback_handler_func)(event_container_t *hset, event_p_t* e_params);
typedef void (*socket_callback_func)(http_socket_event_loop_t *hset, int fd);

struct event_fds_s{
    int fd;             // 要监听连接描述符
    int events;         // Epoll events 要监听的事件类型
    fd_callback_handler_func fd_handler;
    void* args;
    int invoke_num;
};

struct event_use_fds_s {
    int fd;
    int rd;
    int wd;
};

struct http_socket_event_handle_s{
    struct sockaddr_in client_addr; // 客户端的地址信息
    socklen_t sin_size;             // 客户端地址长度
    socket_callback_func func_read;
    socket_callback_func func_write;
    struct http_socket_event_s *hset
};
void init_event_io(struct event_container_s *ev_container);
void create_event_io(struct event_container_s *ev_container);
int add_event(struct event_container_s *ev_container, int fd, uint32_t event, fd_callback_handler_func fd_handler, void *args, int n);
int del_event(struct event_container_s *ev_container, int fd, uint32_t event) ;
int io_cycle(struct event_container_s *ev_container);
void triggerIo(struct event_container_s *ev_container);
void free_event_io(struct event_container_s *ev_container);

int s_add_event(struct event_container_s *ev_container, pthread_mutex_t *lock, int fd, uint32_t event, fd_callback_handler_func fd_handler, void *args, int n);
int s_del_event(struct event_container_s *ev_container, pthread_mutex_t *lock,int fd, uint32_t event);

int initPipe(int *pipefd);
int readPipe(int rpfd, char *buf);
int writePipe(int wpfd, char* buf);
int closePipe(int pfd);

/**************************************************************************/
void pipe_read_fd_handler(event_container_t *ec, event_p_t *e_param);
#endif //HTTPSERVER_HTTP_MULTI_IO_H
